<?php
class ModelCommunityLottery extends Model{
    public function addLotteryWonProduct($data){
        //get customer address
        $address_query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "address WHERE address_id = '" . (int)$this->customer->getAddressId() . "' AND customer_id = '" . (int)$this->customer->getId() . "'");

        $country_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "country` WHERE country_id = '" . (int)$address_query->row['country_id'] . "'");

        if ($country_query->num_rows) {
            $country = $country_query->row['name'];
            $iso_code_2 = $country_query->row['iso_code_2'];
            $iso_code_3 = $country_query->row['iso_code_3'];
            $address_format = $country_query->row['address_format'];
        } else {
            $country = '';
            $iso_code_2 = '';
            $iso_code_3 = '';
            $address_format = '';
        }

        $zone_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "zone` WHERE zone_id = '" . (int)$address_query->row['zone_id'] . "'");

        if ($zone_query->num_rows) {
            $zone = $zone_query->row['name'];
            $code = $zone_query->row['code'];
        } else {
            $zone = '';
            $code = '';
        }

        $address_data = array(
            'firstname'      => $address_query->row['firstname'],
            'lastname'       => $address_query->row['lastname'],
            'company'        => $address_query->row['company'],
            'address_1'      => $address_query->row['address_1'],
            'address_2'      => $address_query->row['address_2'],
            'postcode'       => $address_query->row['postcode'],
            'city'           => $address_query->row['city'],
            'zone_id'        => $address_query->row['zone_id'],
            'zone'           => $zone,
            'zone_code'      => $code,
            'country_id'     => $address_query->row['country_id'],
            'country'        => $country,
            'iso_code_2'     => $iso_code_2,
            'iso_code_3'     => $iso_code_3,
            'address_format' => $address_format
        );

        //add order
        $this->db->query("INSERT INTO `" . DB_PREFIX . "order` SET invoice_prefix = 'Lottery-', store_id = '0', store_name = 'MicBus', store_url = '" . $this->db->escape('http://www.micbus.com') . "', customer_id = '" . (int)$this->customer->getId() . "', customer_group_id = '" . (int)$this->customer->getCustomerGroupId() . "', firstname = '" . $this->db->escape($address_data['firstname']) . "', lastname = '" . $this->db->escape($address_data['lastname']) . "', email = '" . $this->db->escape($this->customer->getEmail()) . "', telephone = '" . $this->db->escape($this->customer->getTelephone()) . "', fax = '" . $this->db->escape($this->customer->getFax()) . "', payment_firstname = '" . $this->db->escape($address_data['firstname']) . "', payment_lastname = '" . $this->db->escape($address_data['lastname']) . "', payment_company = '" . $this->db->escape($address_data['company']) . "', payment_address_1 = '" . $this->db->escape($address_data['address_1']) . "', payment_address_2 = '" . $this->db->escape($address_data['address_2']) . "', payment_city = '" . $this->db->escape($address_data['city']) . "', payment_postcode = '" . $this->db->escape($address_data['postcode']) . "', payment_country = '" . $this->db->escape($address_data['country']) . "', payment_country_id = '" . (int)$address_data['country_id'] . "', payment_zone = '" . $this->db->escape($address_data['zone']) . "', payment_zone_id = '" . (int)$address_data['zone_id'] . "', payment_address_format = '', payment_code = '" . $this->db->escape('free_checkout') . "', payment_method = '" . $this->db->escape('Free Checkout') . "', language_id = '1', currency_id = '2', currency_code = 'USD', comment='Lottery Prize', currency_value = '1', order_status_id='1', date_added = NOW(), date_modified = NOW()");

        $order_id = $this->db->getLastId();

        $this->db->query("INSERT INTO " . DB_PREFIX . "order_product SET order_id = '" . (int)$order_id . "', product_id = '" . (int)$data['prize'] . "', name = '" . $this->db->escape($data['item']) . "', model = '', quantity = '1', price = '0.0000', total = '0', tax = '0', reward = '0'");


        $this->db->query("INSERT INTO " . DB_PREFIX . "order_total SET order_id = '" . (int)$order_id . "', code = '" . $this->db->escape('sub_total') . "', title = '" . $this->db->escape('Sub-Total') . "', text = '" . $this->db->escape('$0.00') . "', `value` = '0', sort_order = '1'");

        $this->db->query("INSERT INTO " . DB_PREFIX . "order_total SET order_id = '" . (int)$order_id . "', code = '" . $this->db->escape('total') . "', title = '" . $this->db->escape('Total') . "', text = '" . $this->db->escape('$0.00') . "', `value` = '0', sort_order = '2'");
    }

    public function addLotteryWonCoupon($data){
        $this->db->query("INSERT INTO " . DB_PREFIX . "coupon SET customer_id='" . (int)$this->customer->getId() . "', name = '" . $this->db->escape($this->customer->getFirstName() . '\'s coupon won for the lottery draw on ' . date('Y-m-d')) . "', code = '" . $this->db->escape($this->customer->getId() . date('YmdHis') . mt_rand(0,99)) . "', discount = '" . (float)$data['item'] . "', type = 'P', total = '0', logged = '1', shipping = '0', date_start = '" . $this->db->escape(date('Y-m-d')) . "', date_end = '" . $this->db->escape(date('Y-m-d',time()+60*60*24*30)) . "', uses_total = '1', uses_customer = '1', status = '1', date_added = NOW()");
    }

    public function addLotteryWonRewardPoint($data){
        $this->db->query("INSERT INTO " . DB_PREFIX . "customer_reward SET customer_id = '" . (int)$this->customer->getId() . "', order_id = '0', points = '" . (int)$data['item'] . "', description = '" . $this->db->escape($this->customer->getFirstName() . '\'s reward points won for the lottery draw on ' . date('Y-m-d')) . "', date_added = NOW()");
    }

    public function addLotteryHistoryReducePoints($data,$now_time){
        $this->db->query("INSERT INTO " . DB_PREFIX . "community_lottery_history SET customer_id = '" . (int)$this->customer->getId() . "', prize_id = '" . (int)$data['id'] . "', prize_code = '" . (int)$data['prize'] . "', prize_name = '" . $this->db->escape($data['item']) . "', draw_time = '" . $now_time . "'");

        $this->db->query("INSERT INTO " . DB_PREFIX . "community_forum_point_history SET user_id='" . (int)$this->customer->getForumUserId() . "', type='0', description='" . $this->db->escape('Lottery Draw Cost') . "', points='-" . (int)$this->config->get('lottery_cost') . "', add_time='" . $now_time . "'");

        $this->db->query("UPDATE " . DB_PREFIX . "community_users SET forum_points=forum_points-" . (int)$this->config->get('lottery_cost') . " WHERE user_id='" . (int)$this->customer->getForumUserId() . "'");
    }

    public function getProductData($product_id){
        $query=$this->sql_cache->get('lottery.productdata.' . $product_id . '.' . (int)$this->config->get('config_language_id'));

        if(!$query){
            $query=$this->db->query("SELECT p.image, pd.name FROM " . DB_PREFIX . "product AS p LEFT JOIN " . DB_PREFIX . "product_description AS pd ON (pd.product_id=p.product_id AND pd.language_id='" . (int)$this->config->get('config_language_id') . "') WHERE p.product_id='" . (int)$product_id . "'");

            $this->sql_cache->set('lottery.productdata.' . $product_id . '.' . (int)$this->config->get('config_language_id'), $query);
        }

        return $query->row;
    }

    public function getProductImages($product_id){
        $query=$this->sql_cache->get('lottery.productimages.' . $product_id);

        if(!$query){
            $query=$this->db->query("SELECT image FROM " . DB_PREFIX . "product_image WHERE product_id='" . (int)$product_id . "'");

            $this->sql_cache->set('lottery.productimages.' . $product_id, $query);
        }

        return $query->rows;
    }

    public function getLotteryHistory(){
        $query=$this->db->query("SELECT draw_time FROM " . DB_PREFIX . "community_lottery_history WHERE customer_id = '" . (int)$this->customer->getId() . "' ORDER BY draw_time DESC LIMIT 0,2");

        return $query->rows;
    }

    public function getLatestLotteryWinners(){
        $query=$this->sql_cache->get('lottery.latestlotterywinners');

        if(!$query){
            $query=$this->db->query("SELECT lh.prize_code, lh.prize_name, lh.prize_id, u.username, u.user_id FROM " . DB_PREFIX . "community_lottery_history AS lh LEFT JOIN " . DB_PREFIX . "community_users AS u on(u.customer_id=lh.customer_id) WHERE lh.prize_id<9 ORDER BY lh.draw_time DESC LIMIT 0,8");

            $this->sql_cache->set('lottery.latestlotterywinners', $query, TRUE, 60*60);
        }

        return $query->rows;
    }
}
?>